package com.delivery.controller;


import com.delivery.dto.WXAuthDto;
import com.delivery.entity.User;
import com.delivery.interceptors.JWTInterceptor;
import com.delivery.result.ApiResult;
import com.delivery.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author sixiao
 * @since 2023-04-12
 */
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private IUserService userService;




    //TODO 微信登录
    @GetMapping("/getSessionId")
    public ApiResult<Map<String,String>> getSessionId(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getSessionId(code));
    }

    @PostMapping("/authLogin")
    public ApiResult authLogin(@RequestBody WXAuthDto wxAuthDto){
        String token = userService.authLogin(wxAuthDto);
        return ApiResult.ok("登陆成功!",token);
    }

    @PostMapping("/token/getUserByToken")
    @ApiOperation(value = "获取用户信息",notes = "无需传参")
    public Object getUserByToken(@RequestHeader Map<String,Object> map)  {
        return  ApiResult.ok(userService.getUserByToken(map));
    }

    @PostMapping("/token/updateUser")
    @ApiOperation(value = "更改用户信息",notes = "需传对应用户字段")
    public Object updateUser(@Param("参数为：userName userNick 等") @RequestBody User user){
        String userId = JWTInterceptor.userId;
        user.setUserId(Long.valueOf(userId));

        if( userService.saveOrUpdate(user) ){
            return ApiResult.ok("操作成功！");
        }else {
            return ApiResult.fail("操作失败！");
        }
    }

}
